﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuakeStats
{
    class GameEntry : LogEntry
    {
        public GameEntry(string line)
            : base(line)
        {

        }

        public bool IsWarmup
        { get; set; }

        public GameTypeEnum GameType
        {
            get { return (GameTypeEnum)(int.Parse(_dataValues["g_gametype"])); }
        }

        private DateTime? _endTime;
        public DateTime? EndTime
        {
            get 
            {
                return _endTime;
            }
         }

        public void SetEndDate(string timeString)
        {
            _endTime = Program.MasterDate.AddMinutes(int.Parse(timeString.Split(':')[0])).AddSeconds(int.Parse(timeString.Split(':')[1]));
            if (_dbGame != null)
                _dbGame.EndTime = _endTime;
        }

        public Game DbGame
        {
            get { return _dbGame; }
        }

        public string MapName
        {
            get { return _dataValues["mapname"]; }
        }

        private TeamScoreEntry _teamScore;
        private Game _dbGame;

        public TeamScoreEntry TeamScore
        {
            set
            {
                _teamScore = value;
                if (_dbGame != null)
                {
                    _dbGame.RedScore = int.Parse(_teamScore.TeamRedScore);
                    _dbGame.BlueScore = int.Parse(_teamScore.TeamBlueScore);
                }
            }
        }

        private Dictionary<string, string> _dataValues;


        protected override void ParseLineData(string line)
        {
            //  0:00 InitGame: \g_gametype\4\sv_maxclients\32\version\Q3 1.32 win-x86 Oct  7 2002\dmflags\0\fraglimit\30\timelimit\10\protocol\68\mapname\mpteam1\sv_privateClients\0\sv_hostname\Quakers rule!\sv_punkbuster\0\sv_maxRate\10000\sv_minPing\0\sv_maxPing\0\sv_floodProtect\1\sv_allowDownload\1\g_motd\CTF FTW!\Administrator\Umfundisii\Email\EMAIL OF ADMIN\URL\WEBSITE\Location\SLC\CPU\400MHZ P2\Clan\ICS\Connection\T1\capturelimit\15\g_obeliskRespawnDelay\10\sc_voteflags\5119\gamename\baseq3\g_maxGameClients\0\g_needpass\0\g_redteam\Stroggs\g_blueteam\Pagans\g_enableDust\0\g_enableBreath\0\sc_version\1.6 
            string actualData = line.Trim().Substring(Time.Length + 1).Split(':')[1].Trim().Substring(1);
            _dataValues = CreateDictionary(actualData);
        }

        public void InitGameDb()
        {
            // IF the game is just a warmup, don't log it.  Ignore it.
            if (IsWarmup) return;

            _dbGame = new Game();
            _dbGame.Map = SingleContext.Instance.Maps.Where(g => g.Name == MapName).FirstOrDefault();
            _dbGame.GameTypeId = (int)GameType;
            _dbGame.GameTypeDesc = GameType.ToString();
            _dbGame.StartTime = MasterTime;

            SingleContext.Instance.AddToGames(_dbGame);
            SingleContext.Instance.SaveChanges();
        }
    }
}
